package cn.bdqn.pn.service.base.impl;

import cn.bdqn.pn.pojo.base.Question;
import cn.bdqn.pn.reposity.base.QuestionRepository;
import cn.bdqn.pn.service.base.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionRepository questionRepository;
/*
    @Override
    public Integer questionCount(Integer questionId) {
        return qr.findQuestionByQuestionreplytypeid(questionId);
    }

    @Override
    public Page<Question> getAllQuestionInfo(Question question, Integer pageNo, Integer pageSize) {
        Specification<Question> specification=new Specification<Question>() {
            @Override
            public Predicate toPredicate(Root<Question> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> list=new ArrayList<>();
                *//*if(mainOrder.getOrderNum()!=null && !mainOrder.getOrderNum().equals("")) {
                    list.add(cb.equal(root.get("orderNum"), mainOrder.getOrderNum()));
                }*//*
                Predicate[] p =new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        };

        Pageable pageable=new PageRequest(pageNo=pageNo==null?0:pageNo,pageSize=pageSize==null?2:pageSize,new Sort(new Sort.Order(Sort.Direction.DESC,"orderTime")));
        return qr.findAll(specification,pageable);
    }

    @Override
    public Integer addQuestionByQuestion(Question question) {
        Question question1=qr.save(question);
        if(question1!=null){
            return 1;
        }
        return 0;
    }

    @Override
    public Integer delQuestionByQuestionId(Integer questionId) {
        try{
            qr.delete(questionId);
            return 1;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Integer updQuestionInfo(Integer questionId, Integer questionReplyTypeId) {
        Question question=qr.findOne(questionId);
        question.setQuestionreplytypeid(questionReplyTypeId);
        Question question1=qr.save(question);
        if(question1!=null){
            return 1;
        }
        return 0;
    }*/

    /**
     * 展示所有问题
     * @return
     */
    @Override
    public List<Question> showQuestion() {
       return questionRepository.findAll();
    }
}
